#!/usr/bin/env python
#
# Example of a "control" experiment using SimVOC. This type of experiment
# uses queuing time information from the trace data to match the grid
# behavior observed in the input data sets. Simulated scheduling components
# do not actually run.
#
# Author:   Mike Murphy (mamurph@cs.clemson.edu)
# Revision: 23 September 2009
#
#   Copyright 2009 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

# File paths for I/O
import os, os.path

HOME = os.getenv('HOME')

# Adjust these paths for input/output locations
INPUT_PATH = os.path.join(HOME, 'data/')
OUTPUT_FILE = os.path.join(HOME, 'output/control.sq3')

# Needed simulator components
from simvoc.core.aggregator import Aggregator
from simvoc.core.control_scheduler import new_control_scheduler
from simvoc.core.db_output import DBOutput
from simvoc.core.duplicate_filter import DuplicateFilter
from simvoc.core.filter_results import ResultFilter
from simvoc.core.grid_sim import Grid
from simvoc.core.input_manager import JOB, MAP, VO, flex_loader
from simvoc.core.kernel import Simulator
from simvoc.core.results import kernel_trace, kernel_metadata

# Output pipeline
result_filter = [ 'Aggregation', 'Discard', 'Discrepancy', 'Job', 'Metadata',
                  'Site', 'SiteRejection', 'Trace', 'VO' ]
#
db_out = DBOutput(OUTPUT_FILE)
sel = ResultFilter(result_filter, db_out)
agg = Aggregator(sel)
df = DuplicateFilter(agg)
out_h = df

# Main simulator instance
sim = Simulator(output_handler=out_h, ktrace=kernel_trace, \
                kmeta=kernel_metadata)
#

# Core grid
grid = Grid('EGEE', sim, new_control_scheduler)

# Input file handling
fi = flex_loader(sim, grid, {})
fi.load(INPUT_PATH, JOB, extension='.jobs')
fi.load(INPUT_PATH, MAP, extension='.map')
fi.load(INPUT_PATH, VO, extension='.vo')
fi.normalize()

# Actual execution
sim.run()

# Cleanup
fi.close()
out_h.close()
